package com.wbp.cms.core.exception;

import com.wbp.cms.client.result.Result;

public class BaseRuntimeException extends RuntimeException {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private boolean showErrMsgOut = false;
	private String errmsg;
	
	public BaseRuntimeException() {
		super();
	}
	
	public BaseRuntimeException(Throwable cause) {
		super(cause);
	}
	
	public BaseRuntimeException(String message) {
		super(message);
		this.errmsg = message;
	}
	
	public BaseRuntimeException(String message, Throwable cause) {
		super(message, cause);
		this.errmsg = message;
	}
	
	/**
	 * @return the showErrMsgOut
	 */
	public boolean getShowErrMsgOut() {
		return showErrMsgOut;
	}
	
	/**
	 * 把异常消息直接抛出去，可能给前端直接显示给用户，所以设置这个需要慎重
	 * 
	 * @param showErrMsgOut
	 *            the showErrMsgOut to set
	 */
	public BaseRuntimeException setShowErrMsgOut(boolean showErrMsgOut) {
		this.showErrMsgOut = showErrMsgOut;
		return this;
	}
	
	/**
	 * 获取当前异常对外暴漏的error result，包括对外显示的信息是使用通用默认的还是个性化配置的
	 * 不经过ExceptionHandlerAdvice处理，直接send出去
	 * @return
	 */
	public Object sendErrResult() {
	   if (this instanceof ServiceException) {
			return Result.buildError(ErrorType.ERR_BUSINESS.getCode(),getErrMsgShowOut(ErrorType.ERR_BUSINESS));
		} else if (this instanceof SystemException) {
			return Result.buildError(ErrorType.ERR_SYSTEM.getCode(), getErrMsgShowOut(ErrorType.ERR_SYSTEM));
		} else if (this instanceof CheckException) {
			return Result.buildError(ErrorType.ERR_SYSTEM.getCode(), getErrMsgShowOut(ErrorType.ERR_SYSTEM));
		} else {
			return Result.buildError(ErrorType.ERR_SYSTEM.getCode(), getErrMsgShowOut(ErrorType.ERR_SYSTEM));
		}
	}
	
	private String getErrMsgShowOut(ErrorType errorType) {
		if (showErrMsgOut) {
			return errmsg;
		}
		return errorType.getMessage();
	}
	
}
